package com.qs.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.infomanage.bean.FieldBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.BaseDAO;
import com.infomanage.exception.DaoException;
import com.infomanage.po.Department;
import com.infomanage.po.HibernateSessionFactory;
import com.infomanage.po.Person;
import com.jiuqi.webtree.util.StringUtils;
import com.qs.po.QsRpTest;
import com.qs.vo.QsRpTestItem;


public class QsRpTestDAO extends BaseDAO<QsRpTest> {

	@SuppressWarnings("unchecked")
	public QsRpTestItem find(int id) throws DaoException {
		try {
			SearchBean searchBean  = new SearchBean();
			FieldBean fieldBean  = new FieldBean();
			fieldBean.setName("id");
			fieldBean.setValue(id);
			searchBean.add(fieldBean);
			List result = super.findByFields(new Class[] {Person.class, Department.class },
					QsRpTest.class, new String[] {"personId", "deptId" },
					new String[] {"trainee", "deptId" }, searchBean);
			List<QsRpTestItem> items = parseResult(result);
			return items.size() == 0 ? null : items.get(0);
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<QsRpTestItem> findAll() throws DaoException {
		try {
			List result = findAllMutilTable(new Class[] {Person.class, Department.class },
					QsRpTest.class, new String[] {"personId", "deptId" },
					new String[] {"trainee", "deptId" });
			return parseResult(result);
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	
	
	public List<QsRpTestItem> findByTraineeAndDate(String[] traineeIds,
			String beginDate, String endDate) throws DaoException {
		if(traineeIds == null || traineeIds.length == 0) {
			return null;
		}
		String hsql = "select * from Qs_Ao_Test"
				+ " as T1 inner join person"
				+ " as T2 on T1.person_id = T2.person_Id " 
				+ " inner join department as T3 on T1.dept_Id = T3.dept_Id " 
				+ " where T1.person_id in (";
		StringBuilder sb = new StringBuilder(hsql);
		for (int i = 0; i < traineeIds.length; i++) {
			if(StringUtils.isNotEmpty(traineeIds[i])) {
				if(i != 0) {
					sb.append(",");
				}
				sb.append(traineeIds[i]);
			}
		}
		sb.append(")");
		sb.append(" and qs_aot_Date >= '" + beginDate + "'");
		sb.append(" and qs_aot_Date <= '" + endDate + "'");
		sb.append(" order by qs_aot_Date");
		List result = super.findBySQL(sb.toString(), new Class[] {QsRpTest.class, Person.class, Department.class});
		Iterator it = result.iterator();
		List<QsRpTestItem> list = new ArrayList<QsRpTestItem>();
		while (it.hasNext()) {
			Object[] o = (Object[]) it.next();
			QsRpTest bean = (QsRpTest) o[0];
			Person trainee = (Person) o[1];
			Department dept = (Department) o[2];
			QsRpTestItem item = new QsRpTestItem();
			item.setBean(bean);
			item.setPerson(trainee);
			item.setDept(dept);
			list.add(item);
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<QsRpTestItem> findByFields(SearchBean searchBean) throws DaoException {
		List result = super.findByFields(new Class[] {Person.class, Department.class },
				QsRpTest.class, new String[] {"personId", "deptId" },
				new String[] {"trainee", "deptId" }, searchBean);
		return parseResult(result);
	}
	
	@SuppressWarnings("unchecked")
	private List<QsRpTestItem> parseResult(List result){
		Iterator it = result.iterator();
		List<QsRpTestItem> list = new ArrayList<QsRpTestItem>();
		while (it.hasNext()) {
			Object[] o = (Object[]) it.next();
			QsRpTest bean = (QsRpTest) o[0];
			Person person = (Person) o[1];
			Department dept = (Department) o[2];
			QsRpTestItem item = new QsRpTestItem();
			item.setBean(bean);
			item.setPerson(person);
			item.setDept(dept);
			list.add(item);
		}
		return list;
	}
	
	public int count(SearchBean searchBean){
		return super.count(new Class[] {Person.class, Department.class },
				QsRpTest.class, new String[] {"personId", "deptId" },
				new String[] {"trainee", "deptId" }, searchBean);
	}
}
